﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class CmsContentCollect
    {
        public int Add(KeRui.Model.CmsContentCollect model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4), new SqlParameter("@ContentID", SqlDbType.Int, 4), new SqlParameter("@Title", SqlDbType.NVarChar, 400), new SqlParameter("@NodeName", SqlDbType.NVarChar, 100), new SqlParameter("@Url", SqlDbType.NVarChar, 100), new SqlParameter("@CollectTime", SqlDbType.DateTime), new SqlParameter("@State", SqlDbType.NVarChar, 100), new SqlParameter("@UserID", SqlDbType.Int, 4) };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.ContentID;
            parameters[2].Value = model.Title;
            parameters[3].Value = model.NodeName;
            parameters[4].Value = model.Url;
            parameters[5].Value = model.CollectTime;
            parameters[6].Value = model.State;
            parameters[7].Value = model.UserID;
            DbHelperSQL.RunProcedure("CmsContentCollect_ADD", parameters, out num);
            return (int) parameters[0].Value;
        }

        public void Delete(int ID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            parameters[0].Value = ID;
            DbHelperSQL.RunProcedure("CmsContentCollect_Delete", parameters, out num);
        }

        public KeRui.Model.CmsContentCollect GetModel(int ContentID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ContentID", SqlDbType.Int, 4) };
            parameters[0].Value = ContentID;
            KeRui.Model.CmsContentCollect collect = new KeRui.Model.CmsContentCollect();
            DataSet set = DbHelperSQL.RunProcedure("CmsContentCollect_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    collect.ID = int.Parse(set.Tables[0].Rows[0]["ID"].ToString());
                }
                if (set.Tables[0].Rows[0]["ContentID"].ToString() != "")
                {
                    collect.ContentID = int.Parse(set.Tables[0].Rows[0]["ContentID"].ToString());
                }
                collect.Title = set.Tables[0].Rows[0]["Title"].ToString();
                collect.NodeName = set.Tables[0].Rows[0]["NodeName"].ToString();
                collect.Url = set.Tables[0].Rows[0]["Url"].ToString();
                if (set.Tables[0].Rows[0]["CollectTime"].ToString() != "")
                {
                    collect.CollectTime = DateTime.Parse(set.Tables[0].Rows[0]["CollectTime"].ToString());
                }
                collect.State = set.Tables[0].Rows[0]["State"].ToString();
                if (set.Tables[0].Rows[0]["UserID"].ToString() != "")
                {
                    collect.UserID = int.Parse(set.Tables[0].Rows[0]["UserID"].ToString());
                }
                return collect;
            }
            return null;
        }
    }
}

